const path = require("path");
const config = require("../config");
const { logger } = require("../../middleware/logger");

/**
 * 处理静态资源的配置参数
 * @param {*} app 
 * @param {*} static 
 */
function staticInit(app, static) {
  if (config.static) {
    let publcPath = [];
    let _path;
    // 若是字符串的则进行简单的处理
    if (config.static instanceof String) {
      _path = `${path.join(__dirname, "../../", config.static)},`;
      app.use(static(_path));
      publcPath.push(_path);
    } else if (config.static instanceof Array) {
      // 若 config.static 为数组 则进行遍历处理
      config.static.map(item => {
        if(typeof item === 'string') {
          app.use(static(item));
          publcPath.push(item);
        } else if (typeof item === 'object') {
          app.use(item.name, static(item.path));
          publcPath.push(item.path);
        }
      })
    }

    // 打印配置文件中已经开放访问的文件夹名称
    const rootPath = path.resolve(__dirname, "../../");
    publcPath = publcPath.map(item => {
      item = item.replace(rootPath, '')
      return item;
    })
    logger(`开放公共配置地址：${publcPath.join(",")}`)
  }
}

module.exports = staticInit